package org.jeecg.modules.htxincailiao.patent.controller;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSONObject;
import org.apache.commons.lang3.StringUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.constant.CommonConstant;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.system.util.JwtUtil;
import org.jeecg.common.util.oConvertUtils;
import org.jeecg.modules.htxincailiao.account.entity.HTMerchant;
import org.jeecg.modules.htxincailiao.account.entity.HTPlatformUser;
import org.jeecg.modules.htxincailiao.account.service.IHTAccountService;
import org.jeecg.modules.htxincailiao.account.service.IHTMerchantService;
import org.jeecg.modules.htxincailiao.address.entity.HTShopAddress;
import org.jeecg.modules.htxincailiao.address.service.IHTShopAddressService;
import org.jeecg.modules.htxincailiao.patent.entity.HTPatentSupply;
import org.jeecg.modules.htxincailiao.patent.entity.HTPatentSupplyInquiry;
import org.jeecg.modules.htxincailiao.patent.service.IHTPatentSupplyInquiryService;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;

import org.jeecg.modules.htxincailiao.patent.service.IHTPatentSupplyService;
import org.jeecg.modules.htxincailiao.shop.entity.HTShop;
import org.jeecg.modules.htxincailiao.shop.service.IHTShopService;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.modules.permissions.HTRolePermission;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.jeecg.common.aspect.annotation.AutoLog;

/**
 * @Description: ht_patent_supply_inquiry
 * @Author: jeecg-boot
 * @Date:   2021-07-04
 * @Version: V1.0
 */
@Api(tags="ht_patent_supply_inquiry")
@RestController
@RequestMapping("/patent/hTPatentSupplyInquiry")
@Slf4j
public class HTPatentSupplyInquiryController extends JeecgController<HTPatentSupplyInquiry, IHTPatentSupplyInquiryService> {
	@Resource
	private IHTPatentSupplyInquiryService hTPatentSupplyInquiryService;
	@Resource
	private IHTPatentSupplyService patentSupplyService;
	@Resource
	private IHTAccountService accountService;

	@Resource
	private IHTMerchantService merchantService;

	@Resource
	private IHTShopAddressService shopAddressService;


	/**
	 * 分页列表查询
	 *
	 * @param hTPatentSupplyInquiry
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	@AutoLog(value = "ht_patent_supply_inquiry-分页列表查询")
	@ApiOperation(value="ht_patent_supply_inquiry-分页列表查询", notes="ht_patent_supply_inquiry-分页列表查询")
	@GetMapping(value = "/list")
	public Result<?> queryPageList(HTPatentSupplyInquiry hTPatentSupplyInquiry,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
		JSONObject obj = HTRolePermission.GetShopPermissionInfo(req);
		if (obj.containsKey("shopId") && StringUtils.isNotBlank(obj.getString("shopId"))) {
			hTPatentSupplyInquiry.setShopId(obj.getString("shopId"));
		}
		QueryWrapper<HTPatentSupplyInquiry> queryWrapper = QueryGenerator.initQueryWrapper(hTPatentSupplyInquiry, req.getParameterMap());
		Page<HTPatentSupplyInquiry> page = new Page<HTPatentSupplyInquiry>(pageNo, pageSize);
		IPage<HTPatentSupplyInquiry> pageList = hTPatentSupplyInquiryService.page(page, queryWrapper);
		return Result.OK(pageList);
	}

	/**
	 *   添加
	 *
	 * @param hTPatentSupplyInquiry
	 * @return
	 */
	@AutoLog(value = "ht_patent_supply_inquiry-添加")
	@ApiOperation(value="ht_patent_supply_inquiry-添加", notes="ht_patent_supply_inquiry-添加")
	@PostMapping(value = "/add")
	public Result<?> add(HttpServletRequest request, @RequestBody HTPatentSupplyInquiry hTPatentSupplyInquiry) {
		//判断询价者是否登录
		String token = request.getHeader(CommonConstant.X_ACCESS_TOKEN);
		if (oConvertUtils.isEmpty(token)) {
			return Result.error("报价用户未登录！");
		}

		//获取询价者用户信息, 更新询价者ID
		String username = JwtUtil.getUsername(token);
		HTPlatformUser inquiryUser = accountService.queryFrontendUserByUsername(username);
		hTPatentSupplyInquiry.setInquiryUserId(inquiryUser.getId());

		// 当前专利需求已询价实体 findAll
		// 同一个询价者只能询价一次
		List<HTPatentSupplyInquiry> list = hTPatentSupplyInquiryService.queryPatentSupplyInquiryBy(hTPatentSupplyInquiry.getPatentSupplyId());
		List<HTPatentSupplyInquiry> inquiryList = list.stream( ).filter(inquiry -> inquiry.getInquiryUserId().equals(hTPatentSupplyInquiry.getInquiryUserId())).collect(Collectors.toList());
		if (!inquiryList.isEmpty()) {
			return Result.error("不能重复询价");
		}

		//获取需求发布者店铺
		HTMerchant merchant = merchantService.getById(hTPatentSupplyInquiry.getShopId());
		if (merchant != null) {
			hTPatentSupplyInquiry.setSupplyUserId(merchant.getId());
			hTPatentSupplyInquiry.setSupplyPhoneNumber(merchant.getPhoneNumber());
		}

		boolean succeed =hTPatentSupplyInquiryService.save(hTPatentSupplyInquiry);
		//更新询价企业数
		if (succeed){
			List<HTPatentSupplyInquiry> lastList = hTPatentSupplyInquiryService.queryPatentSupplyInquiryBy(hTPatentSupplyInquiry.getPatentSupplyId());
			HTPatentSupply supply = patentSupplyService.getById(hTPatentSupplyInquiry.getPatentSupplyId());
			supply.setInquiryEnterpriseCount(lastList.size());
			patentSupplyService.updateById(supply);
		}

		return Result.OK("添加成功！");
	}

	/**
	 *  编辑
	 *
	 * @param hTPatentSupplyInquiry
	 * @return
	 */
	@AutoLog(value = "ht_patent_supply_inquiry-编辑")
	@ApiOperation(value="ht_patent_supply_inquiry-编辑", notes="ht_patent_supply_inquiry-编辑")
	@PutMapping(value = "/edit")
	public Result<?> edit(@RequestBody HTPatentSupplyInquiry hTPatentSupplyInquiry) {
		hTPatentSupplyInquiryService.updateById(hTPatentSupplyInquiry);
		return Result.OK("编辑成功!");
	}

	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "ht_patent_supply_inquiry-通过id删除")
	@ApiOperation(value="ht_patent_supply_inquiry-通过id删除", notes="ht_patent_supply_inquiry-通过id删除")
	@DeleteMapping(value = "/delete")
	public Result<?> delete(@RequestParam(name="id",required=true) String id) {
		hTPatentSupplyInquiryService.removeById(id);
		return Result.OK("删除成功!");
	}

	/**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "ht_patent_supply_inquiry-批量删除")
	@ApiOperation(value="ht_patent_supply_inquiry-批量删除", notes="ht_patent_supply_inquiry-批量删除")
	@DeleteMapping(value = "/deleteBatch")
	public Result<?> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.hTPatentSupplyInquiryService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.OK("批量删除成功!");
	}

	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "ht_patent_supply_inquiry-通过id查询")
	@ApiOperation(value="ht_patent_supply_inquiry-通过id查询", notes="ht_patent_supply_inquiry-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<?> queryById(@RequestParam(name="id",required=true) String id) {
		HTPatentSupplyInquiry hTPatentSupplyInquiry = hTPatentSupplyInquiryService.getById(id);
		if(hTPatentSupplyInquiry==null) {
			return Result.error("未找到对应数据");
		}
		return Result.OK(hTPatentSupplyInquiry);
	}

	/**
	 * 导出excel
	 *
	 * @param request
	 * @param hTPatentSupplyInquiry
	 */
	@RequestMapping(value = "/exportXls")
	public ModelAndView exportXls(HttpServletRequest request, HTPatentSupplyInquiry hTPatentSupplyInquiry) {
		return super.exportXls(request, hTPatentSupplyInquiry, HTPatentSupplyInquiry.class, "ht_patent_supply_inquiry");
	}

	/**
	 * 通过excel导入数据
	 *
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/importExcel", method = RequestMethod.POST)
	public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
		return super.importExcel(request, response, HTPatentSupplyInquiry.class);
	}

}
